package main

import (
	"fmt"
	"sync"
	"time"
)

var locker = new(sync.Mutex)
var cond = sync.NewCond(locker)

func main() {
	for i := 0; i < 30; i++ {
		go func(x int) {
			cond.L.Lock()         //獲取鎖
			defer cond.L.Unlock() //釋放鎖

			//time.Sleep(time.Second * 1)
			cond.Wait()           //等待通知,阻塞當前goroutine
			fmt.Println(x)

		}(i)
	}
	//time.Sleep(time.Second * 1)
	fmt.Println("Signal1...")
	cond.Signal() // 下發一個通知給已經獲取鎖的goroutine
	time.Sleep(time.Second * 1)

	fmt.Println("Signal2...")
	cond.Signal() // 1秒之後 下發一個通知給已經獲取鎖的goroutine
	time.Sleep(time.Second * 1)

	fmt.Println("Broadcast...")
	cond.Broadcast() //3秒之後 下發廣播給所有等待的goroutine
	time.Sleep(time.Second * 5)
}